#include "client.h"

int main(){
	signal(SIGPIPE,  hangle);

	int sockfd, ret;
    struct sockaddr_in seraddr; 


	// 创建监听套接字
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0){
		perror("socket");
		return -1;
	}

	seraddr.sin_family = AF_INET;
	seraddr.sin_port = htons(9999);
	inet_pton(AF_INET, "127.0.0.1", &seraddr.sin_addr.s_addr);

	// 连接服务端
	ret = connect(sockfd, (struct sockaddr*)&seraddr, sizeof(struct sockaddr_in));
	if(ret < 0){
		perror("connect");
		return -1;
	}

	// 发送请求
    menu1();
	
	//关闭套接字
	close(sockfd);
    sockfd = -1;

	return 0;
}


/*一级菜单*/
void menu1(){
	int flag = 1;
	char choose;
	while(flag){
        printf("*****************\n");
		printf("*    1、注册    *\n");
		printf("*    2、登录    *\n");
		printf("*    0、退出    *\n");
        printf("*****************\n");
		printf(">>");
		scanf("%c", &choose); // 读取choose
		while(getchar()!='\n');

		switch(choose){
			case '1': //注册
				reg_req(sockfd);
				break;
			case '2':
                login_req(sockfd);
				break;
			case '0': // 退出
				flag = 0;
				break;
			default:
				printf("输入有误，请重新选择！\n");
				break;
		}
    }
}
/*二级菜单*/
void menu2(){
	int flag = 1;
	char choose;
	while(flag){
        printf("*****************\n");
		printf("*  1、查询单词   *\n");
		printf("* 2、查询历史记录 *\n");
		printf("*    0、退出    *\n");
        printf("*****************\n");
		printf(">>");
		scanf("%c", &choose); // 读取choose
		while(getchar()!='\n');

		switch(choose){
			case '1': // 查询单词
				query_req(sockfd);
				break;
			case '2': // 查询历史记录
                history_req(sockfd);
				break;
			case '0': // 退出
				flag = 0;
				break;
			default:
				printf("输入有误，请重新选择！\n");
				break;
		}
    }
}
/*信号处理函数*/
void hangle(int sig){
	printf("resv sig: %d\n", sig);
}
/*注册请求*/
void reg_req(int sockfd){
	int ret;
	struct user_info  u1;
	printf("请输入账号:");
	scanf("%s", u1.name);
	while(getchar()!='\n');
	printf("请输入密码:");
	scanf("%s", u1.passwd);
	while(getchar()!='\n');

	// 打包
	struct pack* pk = NULL;
	pk = (struct pack*)malloc(sizeof(struct pack)+sizeof(struct user_info));
	assert(pk!= NULL);

	pk->ver = 0;
	pk->type = REG_REQ;
	pk->len = sizeof(struct user_info);
	memcpy(pk->data, &u1, sizeof(struct user_info));

	// 将打包的数据发送给服务端
	ret = write(sockfd, pk, sizeof(struct pack)+sizeof(struct user_info));
	if(ret < 0){
		perror("write");
		exit(-1);
	}

	// 读取结果
	ret = read(sockfd, pk, sizeof(struct pack));
	if(ret < 0){
		perror("read");
		exit(-1);
	}
	else if(ret == 0){
        printf("------------------------------\n");
		printf("服务端退出...\n");
        printf("------------------------------\n");
		exit(-1);
	}

	if(pk->type == REG_OK){
        printf("------------------------------\n");
		printf("注册成功...\n");
        printf("------------------------------\n");
    }
	else if(pk->type == REG_FAIL){
        printf("------------------------------\n");
		printf("注册失败，用户名已经存在...\n");
        printf("------------------------------\n");
	}
	else{
        printf("------------------------------\n");
		printf("服务端数据异常...\n");
        printf("------------------------------\n");
	}

	free(pk);
	return ;
}
/*登录请求*/
void login_req(int sockfd){
	int ret,choose;
	struct user_info  u1;
	printf("请输入账号:");
	scanf("%s", u1.name);
	while(getchar()!='\n');
	printf("请输入密码:");
	scanf("%s", u1.passwd);
	while(getchar()!='\n');

	// 打包
	struct pack* pk = NULL;
	pk = (struct pack*)malloc(sizeof(struct pack)+sizeof(struct user_info));
	assert(pk!= NULL);

	pk->ver = 0;
	pk->type = LOGIN_REQ;
	pk->len = sizeof(struct user_info);
	memcpy(pk->data, &u1, sizeof(struct user_info));

	// 将打包的数据发送给服务端
	ret = write(sockfd, pk, sizeof(struct pack)+sizeof(struct user_info));
	if(ret < 0){
		perror("write");
		exit(-1);
	}

	// 读取结果
	ret = read(sockfd, pk, sizeof(struct pack));
	if(ret < 0){
		perror("read");
		exit(-1);
	}
	else if(ret == 0){
        printf("------------------------------\n");
		printf("服务端退出...\n");
        printf("------------------------------\n");
		exit(-1);
	}

	if(pk->type == LOGIN_OK){
        printf("------------------------------\n");
		printf("登录成功...\n");
        printf("------------------------------\n");
		menu2(); // 二级菜单
	}
	else if(pk->type == LOGIN_FAIL1){
        printf("------------------------------\n");
		printf("登录失败，用户名不存在...\n");
        printf("------------------------------\n");
	}
    else if(pk->type == LOGIN_FAIL2){
        printf("------------------------------\n");
		printf("登录失败，密码错误...\n");
        printf("------------------------------\n");
	}
	else{
        printf("------------------------------\n");
		printf("服务端数据异常.....\n");
        printf("------------------------------\n");
	}

	free(pk);
	return ;
}
/*查询单词请求*/
void query_req(int sockfd){
	char word[50] = "";
	char buf[1024] = "";
	printf("请输入查询的单词\n");
	printf(">>");
	scanf("%s", word);
	while(getchar()!='\n');
	// 打包
	struct pack* pk = NULL;
	pk = (struct pack*)malloc(sizeof(struct pack)+sizeof(word));
	assert(pk!= NULL);

	pk->ver = 0;
	pk->type = QUERY_REQ;
	pk->len = sizeof(word);
	memcpy(pk->data, &word, sizeof(word));

	// 将打包的数据发送给服务端
	ret = write(sockfd, pk, sizeof(struct pack)+sizeof(word));
	if(ret < 0){
		perror("write");
		exit(-1);
	}

	// 读包
	ret = read(sockfd, pk, sizeof(struct pack));
	if(ret < 0){
		perror("read");
		exit(-1);
	}
	else if(ret == 0){
        printf("------------------------------\n");
		printf("服务端退出...\n");
        printf("------------------------------\n");
		exit(-1);
	}
	
	if(pk->type == QUERY_OK){
		// 读结果
		ret = read(sockfd, buf, pk->len);
		if(ret < 0){
			perror("read");
			exit(-1);
		}
		else if(ret == 0){
        	printf("------------------------------\n");
			printf("服务端退出...\n");
        	printf("------------------------------\n");
			exit(-1);
		}
        printf("------------------------------\n");
		printf(">>%s\n",buf);
        printf("------------------------------\n");
		menu();
	}
	else if(pk->type == QUERY_FAIL){
        printf("------------------------------\n");
		printf("查询失败...\n");
        printf("------------------------------\n");
	}
	else{
        printf("------------------------------\n");
		printf("服务端数据异常.....\n");
        printf("------------------------------\n");
	}

	free(pk);
	return ;
}
/*查询历史记录*/
void history_req(int sockfd){
	char word[50] = "";
	char buf[1024] = "";
	printf("请输入查询的单词\n");
	printf(">>");
	scanf("%s", word);
	while(getchar()!='\n');
	// 打包
	struct pack* pk = NULL;
	pk = (struct pack*)malloc(sizeof(struct pack)+sizeof(word));
	assert(pk!= NULL);

	pk->ver = 0;
	pk->type = HISTORY_REQ;
	pk->len = sizeof(word);
	memcpy(pk->data, &word, sizeof(word));

	// 将打包的数据发送给服务端
	ret = write(sockfd, pk, sizeof(struct pack)+sizeof(word));
	if(ret < 0){
		perror("write");
		exit(-1);
	}

	// 读包
	ret = read(sockfd, pk, sizeof(struct pack));
	if(ret < 0){
		perror("read");
		exit(-1);
	}
	else if(ret == 0){
        printf("------------------------------\n");
		printf("服务端退出...\n");
        printf("------------------------------\n");
		exit(-1);
	}
	
	if(pk->type == HISTORY_OK){
		// 读结果
		ret = read(sockfd, buf, pk->len);
		if(ret < 0){
			perror("read");
			exit(-1);
		}
		else if(ret == 0){
        	printf("------------------------------\n");
			printf("服务端退出...\n");
        	printf("------------------------------\n");
			exit(-1);
		}
        printf("------------------------------\n");
		printf(">>%s\n",buf);
        printf("------------------------------\n");
		menu();
	}
	else if(pk->type == HISTORY_FAIL){
        printf("------------------------------\n");
		printf("查询历史记录失败...\n");
        printf("------------------------------\n");
	}
	else{
        printf("------------------------------\n");
		printf("服务端数据异常.....\n");
        printf("------------------------------\n");
	}

	free(pk);
	return ;
}